/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}


/**
 * 日期处理
 * @param type 类型
 */
export function getDate(type) {
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();

	if (type === 'start') {
		year = year - 60;
	} else if (type === 'end') {
		year = year + 2;
	}
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	return `${year}-${month}-${day}`;
}

/**
 * 根据字典值返回字典label
 * @param value 字典值
 * @param dicts 字典list
 */
export function getDict(value, dicts) {
	if (!dicts || !dicts.length) {
		return value
	}
	if (!value) return ''
	let dict = dicts.find(item => item.dictValue == value)
	return dict.dictLabel || value
}

/**
 * 校验数字类型并返回规范数字
 * @param value 字典值
 * @param reg 小数点位数/正则表达式
 */
export function validNum(e, reg = 0) {
	let regList = {
		0: /[^\d]/g
	}
	if (!e.detail.value) return
	if (typeof reg == 'number') {
		return e.detail.value.replace(regList[reg], "");
	} else {
		return e.detail.value.replace(reg, "")
	}
}

/**
 * 根据端类型返回fixed的顶部tab的top值
 */
export function getTop() {
	// #ifdef  H5
	return '44px'
	// #endif
	// #ifndef  H5
	return '0px'
	// #endif
}



export function addDaysToDate(dateStr, days, type) {
	// if(days == '' || days == null) return
	var date = new Date(dateStr);
	date.setDate(date.getDate() + days);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	// 补零操作
	month = month < 10 ? '0' + month : month;
	day = day < 10 ? '0' + day : day;

	if (type) {
		return year + "" + month + "" + day;
	} else {
		return year + "-" + month + "-" + day;
	}
}

// 验证托盘
export function validateTrayCode(code) {
	const pattern = /^T\d{6}$/;
	return pattern.test(code);
}

// 验证料架
export function validateShelvesCode(code) {
	const pattern = /^H\d{6}$/;
	return pattern.test(code);
}